import os
import torch
import torchvision
import numpy as np
import albumentations as A
from albumentations.pytorch import ToTensorV2
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from math import log10, sqrt
import glob
from PIL import Image
import cv2
import tensorflow as tf
import imageio
import time
import winsound
import matplotlib.pyplot as plt
%matplotlib inline
path = 'VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007/JPEGImages' path_save = 'VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007/converted'
all_dir = glob.glob(path + '/*.jpg')
for filename in all_dir: img = cv2.imread(filename) img_y2 = img.resize(288) img_y2.save(path_save+'/y_large/'+(filename[len(filename)-10:])) img_y = img.resize(144) img_y.save(path_save+'/y_mid/'+(filename[len(filename)-10:])) img_x = img.resize(72) img_x.save(path_save+'/x/'+(filename[len(filename)-10:]))
path = 'VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007/converted'
X = []
y_mid = []
y_large = []
X_dir = glob.glob(path + '/x/*.jpg')
y_mid_dir = glob.glob(path + '/y_mid/*.jpg')
y_large_dir = glob.glob(path + '/y_large/*.jpg')
for filename in X_dir:
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
X.append(img)
for filename in y_mid_dir:
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
y_mid.append(img)
for filename in y_large_dir:
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
y_large.append(img)
valid_size= int(0.2*len(X))
x_valid = X[0:valid_size]
x_train=X[valid_size:]
y_mid_valid = y_mid[0:valid_size]
y_mid_train=y_mid[valid_size:]
y_large_valid = y_large[0:valid_size]
y_large_train=y_large[valid_size:]
def plot_sample(X, y_mid, y_large, index):
_, aux = plt.subplots(1, 3, figsize=(20, 10))
aux[0].set_title('Image size 72x72')
aux[0].imshow(X[index])
aux[1].set_title('Image size 144x144')
aux[1].imshow(y_mid[index])
aux[2].set_title('Image size 288x288')
aux[2].imshow(y_large[index])
plot_sample(X, y_mid, y_large,0)
plot_sample(X, y_mid, y_large,1)
class ImageDataSet(Dataset):
def __init__(self, x, y_mid, y_large):
self.x = x
self.y_mid = y_mid
self.y_large = y_large
def __len__(self):
return len(self.x)
def __getitem__(self, idx):
img_x = self.x[idx]
img_y_mid = self.y_mid[idx]
img_y_large = self.y_large[idx]
return img_x.T, img_y_mid.T, img_y_large.T
train_dataset = ImageDataSet(x_train, y_mid_train, y_large_train)
train_dataloader = DataLoader(train_dataset, batch_size= 20, num_workers =0, shuffle=False, pin_memory= False)
print(len(train_dataset))
valid_dataset = ImageDataSet(x_valid, y_mid_valid, y_large_valid)
valid_dataloader = DataLoader(valid_dataset, batch_size= 20, num_workers =0, shuffle=False, pin_memory= False)
print(len(valid_dataset))
4009 1002
use_cuda = torch.has_cuda
if use_cuda:
device = torch.device('cuda:0')
else:
device = torch.device('cpu:0')
device = torch.device('cpu:0')
def train(train_loader, valid_loader, learn_rate, epochs=5, large=False, model=None):
# train part
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learn_rate)
model.train()
epoch_times = []
train_log = []
gif = []
preds = None
valid_preds = None
flag = True
for epoch in range(epochs):
counter1 = 0
start_time = time.process_time()
check = 0
avg_loss = 0
step = 0
psnr_sum = 0
print("starting epoch number ", epoch)
for x_train, y_mid_train, y_large_train in train_loader:
step +=1
if large:
labels = y_large_train
else:
labels = y_mid_train
output = model(x_train.to(device, dtype=torch.float), large)
model.zero_grad()
loss = criterion(output, labels.to(device, dtype=torch.float))
loss.backward() #retain_graph=True
optimizer.step()
avg_loss += loss.item()
if step%20 == 0:
print("Epoch {}......Step: {}/{}....... Average Loss for Epoch: {}".format(epoch, step, len(train_loader), avg_loss/step))
current_time = time.process_time()
print("Epoch {}/{} Done, Total Loss: {}".format(epoch, epochs, avg_loss/len(train_loader)))
print("Time Elapsed for Epoch: {} seconds".format(str(current_time-start_time)))
epoch_times.append(current_time-start_time)
# eval part
tp = 0
valid_loss = 0
model.eval()
for x_valid, y_mid_valid, y_large_valid in valid_loader:
x_valid = x_valid.to(device, dtype=torch.float)
y_mid_valid = y_mid_valid.to(device, dtype=torch.long)
y_large_valid = y_large_valid.to(device, dtype=torch.long)
with torch.no_grad():
output2 = model(x_valid, large)
loss = criterion(output2, y_mid_valid)
valid_loss += loss.item()
output3 = output2
image_pred = output3[0]
image_pred = image_pred.float().cpu().detach().numpy().T
image_original = y_mid_valid[0]
image_original = image_original.float().cpu().detach().numpy().T
tp += (y_mid_valid==output2.to(device)).type(torch.float).sum().item()
mse = np.mean((image_original - image_pred) ** 2)
psnr = 100
if mse != 0:
psnr = 20 * log10(255.0 / sqrt(mse))
psnr_sum += psnr
counter1 = counter1 + 1
if valid_preds is None:
ma = output3
valid_preds = ma.float().cpu().detach()
else:
ma = output3
valid_preds = torch.cat((valid_preds, ma.float().cpu().detach()), dim=0)
if preds is None:
preds = valid_preds
else:
preds = torch.cat((preds, valid_preds), dim=0)
metrics = {
'epoch': epoch,
'train_loss': avg_loss/len(train_loader),
'validation_loss' : valid_loss / len(valid_loader),
'val_preds': valid_preds,
'val_score': tp/len(train_loader.dataset)
}
train_log.append(metrics)
gif.append(preds[0])
print(f'PSNR avg = {psnr_sum/counter1},test_loss = {valid_loss/len(valid_loader):2f}, score = {tp/len(train_loader.dataset)}')
print("Total Training Time: {} seconds".format(str(sum(epoch_times))))
return gif, preds, train_log
def printImages(gif1, preds1):
epo = len(gif1)
rows = (int(epo/5))+1
fig, ax = plt.subplots(rows, 5)
fig.set_size_inches(18.5, 10.5)
fig.suptitle("image through the epochs", fontsize=18)
plt.subplots_adjust(hspace=0.5)
gif13 = gif1
for i in range(len(gif1)):
gif13[i] = torch.tensor(gif1[i]).numpy().T.astype(np.float32)/255.0
ax[int((i)/5), (i%5)].imshow(gif13[i])
str1 = 'epoch number' + str(i+1)
ax[int((i)/5), (i%5)].set_title(str1)
imageio.mimsave('movie2.gif', gif1)
for i in range(10): # i replace len(x_valid) in 10 beacuse it too big
fig = plt.figure()
fig.set_size_inches(18.5, 10.5)
ax = fig.add_subplot(1, 3, 1)
image_x1 = x_valid[i]
image_x1 = image_x1.astype(np.float32)/255.0
imgplot = plt.imshow(image_x1)
ax = fig.add_subplot(1, 3, 2)
image12 = preds1[i]
image12 = image12.numpy().T.astype(np.float32)/255.0
imgplot = plt.imshow(image12)
ax = fig.add_subplot(1, 3, 3)
image_y1 = y_mid_valid[i]
image_y1 = image_y1.astype(np.float32)/255.0
imgplot = plt.imshow(image_y1)
def print_result(tr=None, epochs=5, val_labels=None, test_preds=None):
#print("the auc is:")
#print(auc(torch.from_numpy(pd.get_dummies(val_labels).values), test_preds.data.cpu(), average = 'macro'))
epoch_array = []
tr_loss_array = []
val_loss_array = []
val_score_array = []
for metric in tr:
epoch_array.append(metric['epoch'])
tr_loss_array.append(metric['train_loss'])
val_loss_array.append(metric['validation_loss'])
val_score_array.append( metric['val_score'] )
plt.plot(epoch_array, tr_loss_array, label = "train loss", color="blue" )
plt.plot(epoch_array, val_loss_array, label = "valid loss", color="red" )
plt.xlabel("epoch")
plt.ylabel("value")
plt.legend()
plt.show()
plt.plot(epoch_array, val_score_array, label = "valid score", color="green" )
plt.xlabel("epoch")
plt.ylabel("score")
plt.legend()
plt.show()
class Step2Model(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(in_channels=3,out_channels=64, kernel_size=3, padding=1 ).to(device)
self.conv2 = nn.Conv2d(in_channels=64,out_channels=64, kernel_size=3 , padding=1).to(device)
self.upsample = nn.Upsample(scale_factor=(144/72))
self.conv3 = nn.Conv2d(in_channels=64,out_channels=3, kernel_size=1).to(device)
def forward(self, x, large=False):
out = self.conv1(x)
out = self.conv2(out)
out = self.upsample(out)
out = self.conv3(out)
return out
model2 = Step2Model()
gif2, preds2, metrics2 = train(train_loader=train_dataloader, valid_loader=valid_dataloader, learn_rate=0.001, epochs=5, large=False, model=model2)
starting epoch number 0 Epoch 0......Step: 20/201....... Average Loss for Epoch: 2871.470556640625 Epoch 0......Step: 40/201....... Average Loss for Epoch: 1832.7438247680664 Epoch 0......Step: 60/201....... Average Loss for Epoch: 1423.456027730306 Epoch 0......Step: 80/201....... Average Loss for Epoch: 1216.1200687408448 Epoch 0......Step: 100/201....... Average Loss for Epoch: 1090.0814108276368 Epoch 0......Step: 120/201....... Average Loss for Epoch: 999.6085454305013 Epoch 0......Step: 140/201....... Average Loss for Epoch: 933.985956464495 Epoch 0......Step: 160/201....... Average Loss for Epoch: 887.1283069610596 Epoch 0......Step: 180/201....... Average Loss for Epoch: 848.6185184054905 Epoch 0......Step: 200/201....... Average Loss for Epoch: 815.0140962219239 Epoch 0/5 Done, Total Loss: 813.3238025874048 Time Elapsed for Epoch: 290.25 seconds PSNR avg = 21.26132966564679,test_loss = 512.362102, score = 0.010226989274133201 starting epoch number 1 Epoch 1......Step: 20/201....... Average Loss for Epoch: 504.74863586425784 Epoch 1......Step: 40/201....... Average Loss for Epoch: 497.16451263427734 Epoch 1......Step: 60/201....... Average Loss for Epoch: 493.47992045084635 Epoch 1......Step: 80/201....... Average Loss for Epoch: 495.38197860717776 Epoch 1......Step: 100/201....... Average Loss for Epoch: 497.7093557739258 Epoch 1......Step: 120/201....... Average Loss for Epoch: 494.63193283081057 Epoch 1......Step: 140/201....... Average Loss for Epoch: 492.57610299246653 Epoch 1......Step: 160/201....... Average Loss for Epoch: 494.1685773849487 Epoch 1......Step: 180/201....... Average Loss for Epoch: 494.289357164171 Epoch 1......Step: 200/201....... Average Loss for Epoch: 492.34034942626954 Epoch 1/5 Done, Total Loss: 492.1366264381219 Time Elapsed for Epoch: 284.890625 seconds PSNR avg = 21.5624663104037,test_loss = 480.555543, score = 0.006734846595160888 starting epoch number 2 Epoch 2......Step: 20/201....... Average Loss for Epoch: 474.3679595947266 Epoch 2......Step: 40/201....... Average Loss for Epoch: 470.29712677001953 Epoch 2......Step: 60/201....... Average Loss for Epoch: 469.5641067504883 Epoch 2......Step: 80/201....... Average Loss for Epoch: 473.88537178039553 Epoch 2......Step: 100/201....... Average Loss for Epoch: 478.4214178466797 Epoch 2......Step: 120/201....... Average Loss for Epoch: 477.3120325724284 Epoch 2......Step: 140/201....... Average Loss for Epoch: 476.8902644566127 Epoch 2......Step: 160/201....... Average Loss for Epoch: 479.83736476898196 Epoch 2......Step: 180/201....... Average Loss for Epoch: 481.1317526923286 Epoch 2......Step: 200/201....... Average Loss for Epoch: 480.21673110961916 Epoch 2/5 Done, Total Loss: 480.0634278254723 Time Elapsed for Epoch: 290.640625 seconds PSNR avg = 21.58148522369407,test_loss = 478.233595, score = 0.005986530306809678 starting epoch number 3 Epoch 3......Step: 20/201....... Average Loss for Epoch: 472.1168930053711 Epoch 3......Step: 40/201....... Average Loss for Epoch: 468.125749206543 Epoch 3......Step: 60/201....... Average Loss for Epoch: 467.43819936116535 Epoch 3......Step: 80/201....... Average Loss for Epoch: 471.91836471557616 Epoch 3......Step: 100/201....... Average Loss for Epoch: 476.556455078125 Epoch 3......Step: 120/201....... Average Loss for Epoch: 475.50916315714517 Epoch 3......Step: 140/201....... Average Loss for Epoch: 475.196157182966 Epoch 3......Step: 160/201....... Average Loss for Epoch: 478.2166290283203 Epoch 3......Step: 180/201....... Average Loss for Epoch: 479.5620295206706 Epoch 3......Step: 200/201....... Average Loss for Epoch: 478.6912608337402 Epoch 3/5 Done, Total Loss: 478.5495069513274 Time Elapsed for Epoch: 298.203125 seconds PSNR avg = 21.58573219620256,test_loss = 477.579777, score = 0.0074831628835120975 starting epoch number 4 Epoch 4......Step: 20/201....... Average Loss for Epoch: 472.8452774047852 Epoch 4......Step: 40/201....... Average Loss for Epoch: 468.24970321655275 Epoch 4......Step: 60/201....... Average Loss for Epoch: 467.2250244140625 Epoch 4......Step: 80/201....... Average Loss for Epoch: 471.6550178527832 Epoch 4......Step: 100/201....... Average Loss for Epoch: 476.3040939331055 Epoch 4......Step: 120/201....... Average Loss for Epoch: 475.20691808064777 Epoch 4......Step: 140/201....... Average Loss for Epoch: 474.8420839582171 Epoch 4......Step: 160/201....... Average Loss for Epoch: 477.8655874252319 Epoch 4......Step: 180/201....... Average Loss for Epoch: 479.19898613823784 Epoch 4......Step: 200/201....... Average Loss for Epoch: 478.3035342407227 Epoch 4/5 Done, Total Loss: 478.16066442081586 Time Elapsed for Epoch: 329.203125 seconds PSNR avg = 21.61085724743575,test_loss = 474.994786, score = 0.006734846595160888 Total Training Time: 1493.1875 seconds
printImages(gif2, preds2)
<ipython-input-7-cda49081dc91>:10: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). gif13[i] = torch.tensor(gif1[i]).numpy().T.astype(np.float32)/255.0 Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Lossy conversion from float32 to uint8. Range [-0.00987954717129469, 1.034487247467041]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.00987954717129469, 1.034487247467041]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.00987954717129469, 1.034487247467041]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.00987954717129469, 1.034487247467041]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.00987954717129469, 1.034487247467041]. Convert image to uint8 prior to saving to suppress this warning. Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
print_result(tr=metrics2, epochs=5)
class Step3Model(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(in_channels=3,out_channels=64, kernel_size=3, padding=1 ).to(device)
self.conv2 = nn.Conv2d(in_channels=64,out_channels=64, kernel_size=3 , padding=1).to(device)
self.upsample = nn.Upsample(scale_factor=(144/72))
self.conv3 = nn.Conv2d(in_channels=64,out_channels=3, kernel_size=1 ).to(device)
self.upsample2 = nn.Upsample(scale_factor=(288/144))
def forward(self, x, large=False):
out= self.conv1(x)
out= self.conv2(out)
out= self.upsample(out)
if large:
out = self.upsample2(out)
out= self.conv3(out)
return out
model3 = Step3Model()
gif3, preds3, metrics3 = train(train_loader=train_dataloader, valid_loader=valid_dataloader, learn_rate=0.001, epochs=5, large=False, model=model3)
starting epoch number 0 Epoch 0......Step: 20/201....... Average Loss for Epoch: 2617.1402954101563 Epoch 0......Step: 40/201....... Average Loss for Epoch: 1706.575491333008 Epoch 0......Step: 60/201....... Average Loss for Epoch: 1344.761382039388 Epoch 0......Step: 80/201....... Average Loss for Epoch: 1161.1228355407716 Epoch 0......Step: 100/201....... Average Loss for Epoch: 1049.035796508789 Epoch 0......Step: 120/201....... Average Loss for Epoch: 967.7161801656088 Epoch 0......Step: 140/201....... Average Loss for Epoch: 908.5562840053013 Epoch 0......Step: 160/201....... Average Loss for Epoch: 866.3782695770263 Epoch 0......Step: 180/201....... Average Loss for Epoch: 831.3998167249891 Epoch 0......Step: 200/201....... Average Loss for Epoch: 800.5112057495118 Epoch 0/5 Done, Total Loss: 798.9262406837881 Time Elapsed for Epoch: 311.453125 seconds PSNR avg = 21.180757269920488,test_loss = 521.149234, score = 0.006984285357944624 starting epoch number 1 Epoch 1......Step: 20/201....... Average Loss for Epoch: 512.9354568481446 Epoch 1......Step: 40/201....... Average Loss for Epoch: 504.44980697631837 Epoch 1......Step: 60/201....... Average Loss for Epoch: 499.97008514404297 Epoch 1......Step: 80/201....... Average Loss for Epoch: 501.23225059509275 Epoch 1......Step: 100/201....... Average Loss for Epoch: 502.910361328125 Epoch 1......Step: 120/201....... Average Loss for Epoch: 499.2338437398275 Epoch 1......Step: 140/201....... Average Loss for Epoch: 496.6769701276507 Epoch 1......Step: 160/201....... Average Loss for Epoch: 497.7856782913208 Epoch 1......Step: 180/201....... Average Loss for Epoch: 497.51050635443795 Epoch 1......Step: 200/201....... Average Loss for Epoch: 495.1979216003418 Epoch 1/5 Done, Total Loss: 494.9796227602223 Time Elapsed for Epoch: 302.625 seconds PSNR avg = 21.57468607633249,test_loss = 479.095652, score = 0.008979795460214518 starting epoch number 2 Epoch 2......Step: 20/201....... Average Loss for Epoch: 474.1786163330078 Epoch 2......Step: 40/201....... Average Loss for Epoch: 469.933154296875 Epoch 2......Step: 60/201....... Average Loss for Epoch: 469.0994496663412 Epoch 2......Step: 80/201....... Average Loss for Epoch: 473.38576316833496 Epoch 2......Step: 100/201....... Average Loss for Epoch: 477.9334548950195 Epoch 2......Step: 120/201....... Average Loss for Epoch: 476.80870615641277 Epoch 2......Step: 140/201....... Average Loss for Epoch: 476.4372916085379 Epoch 2......Step: 160/201....... Average Loss for Epoch: 479.40455093383787 Epoch 2......Step: 180/201....... Average Loss for Epoch: 480.73099009195965 Epoch 2......Step: 200/201....... Average Loss for Epoch: 479.8790835571289 Epoch 2/5 Done, Total Loss: 479.73232819666316 Time Elapsed for Epoch: 320.578125 seconds PSNR avg = 21.566098892620136,test_loss = 479.630500, score = 0.005238214018458469 starting epoch number 3 Epoch 3......Step: 20/201....... Average Loss for Epoch: 473.74428100585936 Epoch 3......Step: 40/201....... Average Loss for Epoch: 469.28600082397463 Epoch 3......Step: 60/201....... Average Loss for Epoch: 468.15428415934247 Epoch 3......Step: 80/201....... Average Loss for Epoch: 472.4974876403809 Epoch 3......Step: 100/201....... Average Loss for Epoch: 477.2522857666016 Epoch 3......Step: 120/201....... Average Loss for Epoch: 476.41249796549477 Epoch 3......Step: 140/201....... Average Loss for Epoch: 475.9839521135603 Epoch 3......Step: 160/201....... Average Loss for Epoch: 478.9585300445557 Epoch 3......Step: 180/201....... Average Loss for Epoch: 480.2711159600152 Epoch 3......Step: 200/201....... Average Loss for Epoch: 479.42157287597655 Epoch 3/5 Done, Total Loss: 479.28631227407885 Time Elapsed for Epoch: 321.28125 seconds PSNR avg = 21.58876807156768,test_loss = 477.322663, score = 0.006485407832377151 starting epoch number 4 Epoch 4......Step: 20/201....... Average Loss for Epoch: 473.4688232421875 Epoch 4......Step: 40/201....... Average Loss for Epoch: 468.413892364502 Epoch 4......Step: 60/201....... Average Loss for Epoch: 467.4221878051758 Epoch 4......Step: 80/201....... Average Loss for Epoch: 472.12969665527345 Epoch 4......Step: 100/201....... Average Loss for Epoch: 476.77904815673827 Epoch 4......Step: 120/201....... Average Loss for Epoch: 475.63896967569985 Epoch 4......Step: 140/201....... Average Loss for Epoch: 475.2942559378488 Epoch 4......Step: 160/201....... Average Loss for Epoch: 478.29801120758054 Epoch 4......Step: 180/201....... Average Loss for Epoch: 479.813298034668 Epoch 4......Step: 200/201....... Average Loss for Epoch: 479.0008094787598 Epoch 4/5 Done, Total Loss: 478.8559003991274 Time Elapsed for Epoch: 360.6875 seconds PSNR avg = 21.599509146686145,test_loss = 476.196691, score = 0.007233724120728361 Total Training Time: 1616.625 seconds
printImages(gif3, preds3)
<ipython-input-7-cda49081dc91>:10: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). gif13[i] = torch.tensor(gif1[i]).numpy().T.astype(np.float32)/255.0 Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Lossy conversion from float32 to uint8. Range [-0.025835340842604637, 1.0222660303115845]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.025835340842604637, 1.0222660303115845]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.025835340842604637, 1.0222660303115845]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.025835340842604637, 1.0222660303115845]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.025835340842604637, 1.0222660303115845]. Convert image to uint8 prior to saving to suppress this warning. Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
print_result(tr=metrics3, epochs=5)
class Step4Model(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(in_channels=3,out_channels=32, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(in_channels=32,out_channels=32, kernel_size=3, padding=1, dilation=1)
self.conv3 = nn.Conv2d(in_channels=32,out_channels=32, kernel_size=3, padding=1, dilation=1)
self.conv4 = nn.Conv2d(in_channels=32,out_channels=32, kernel_size=3, padding=1, dilation=1)
self.conv5 = nn.Conv2d(in_channels=32,out_channels=32, kernel_size=3, padding=1, dilation=1)
self.conv6 = nn.Conv2d(in_channels=32,out_channels=32, kernel_size=3, padding=1, dilation=1)
self.conv7 = nn.Conv2d(in_channels=32,out_channels=32, kernel_size=3, padding=1, dilation=1)
self.upsample = nn.Upsample(scale_factor=(144/72)).to(device) #, mode='bilinear'
self.conv8 = nn.Conv2d(in_channels=32,out_channels=3, kernel_size=1)
self.upsample2 = nn.Upsample(scale_factor=(288/144)).to(device)
self.relu = nn.ReLU()
def forward(self, x, large=False):
out = self.conv1(x)
out1 = self.conv2(out)
out2 = self.conv3(out1)
out_add1 = torch.add(out, out2)
out_relu1 = self.relu(out_add1)
out3 = self.conv4(out_relu1)
out4 = self.conv5(out3)
out_add2 = torch.add(out4, out_relu1)
out_relu2 = self.relu(out_add2)
out5 = self.upsample(out_relu2)
#out = self.relu(out)
if large:
out6 = self.conv6(out5)
out7 = self.conv7(out6)
out_add3 = torch.add(out7, out5)
out_relu3 = self.relu(out_add3)
out5 = self.upsample2(out_relu3)
#out = self.relu(out)
#out = self.drop(out)
out6 = self.conv8(out5)
#out = self.relu(out)
return out6
model4 = Step4Model()
gif4, preds4, metrics4 = train(train_loader=train_dataloader, valid_loader=valid_dataloader, learn_rate=0.001, epochs=5, large=False, model=model4)
starting epoch number 0 Epoch 0......Step: 20/201....... Average Loss for Epoch: 2211.600720214844 Epoch 0......Step: 40/201....... Average Loss for Epoch: 1528.368537902832 Epoch 0......Step: 60/201....... Average Loss for Epoch: 1240.0739313761394 Epoch 0......Step: 80/201....... Average Loss for Epoch: 1078.7604175567626 Epoch 0......Step: 100/201....... Average Loss for Epoch: 975.7123654174804 Epoch 0......Step: 120/201....... Average Loss for Epoch: 899.8882741292317 Epoch 0......Step: 140/201....... Average Loss for Epoch: 845.0001711164202 Epoch 0......Step: 160/201....... Average Loss for Epoch: 805.1895975112915 Epoch 0......Step: 180/201....... Average Loss for Epoch: 773.1579886542427 Epoch 0......Step: 200/201....... Average Loss for Epoch: 744.7353187561035 Epoch 0/5 Done, Total Loss: 743.3547663902169 Time Elapsed for Epoch: 308.15625 seconds PSNR avg = 21.45352092919197,test_loss = 491.228007, score = 0.007233724120728361 starting epoch number 1 Epoch 1......Step: 20/201....... Average Loss for Epoch: 487.69723052978514 Epoch 1......Step: 40/201....... Average Loss for Epoch: 482.4484634399414 Epoch 1......Step: 60/201....... Average Loss for Epoch: 481.0893356323242 Epoch 1......Step: 80/201....... Average Loss for Epoch: 486.4175136566162 Epoch 1......Step: 100/201....... Average Loss for Epoch: 490.20946228027344 Epoch 1......Step: 120/201....... Average Loss for Epoch: 488.2865720113119 Epoch 1......Step: 140/201....... Average Loss for Epoch: 487.04206412179127 Epoch 1......Step: 160/201....... Average Loss for Epoch: 489.99152889251707 Epoch 1......Step: 180/201....... Average Loss for Epoch: 491.03268059624565 Epoch 1......Step: 200/201....... Average Loss for Epoch: 489.6484603881836 Epoch 1/5 Done, Total Loss: 489.4711016754606 Time Elapsed for Epoch: 298.03125 seconds PSNR avg = 21.539296350851746,test_loss = 482.034901, score = 0.007233724120728361 starting epoch number 2 Epoch 2......Step: 20/201....... Average Loss for Epoch: 476.29017486572263 Epoch 2......Step: 40/201....... Average Loss for Epoch: 471.67774505615233 Epoch 2......Step: 60/201....... Average Loss for Epoch: 471.29786224365233 Epoch 2......Step: 80/201....... Average Loss for Epoch: 479.995458984375 Epoch 2......Step: 100/201....... Average Loss for Epoch: 484.8020538330078 Epoch 2......Step: 120/201....... Average Loss for Epoch: 482.9298657735189 Epoch 2......Step: 140/201....... Average Loss for Epoch: 481.9276661464146 Epoch 2......Step: 160/201....... Average Loss for Epoch: 484.408599281311 Epoch 2......Step: 180/201....... Average Loss for Epoch: 485.2721689860026 Epoch 2......Step: 200/201....... Average Loss for Epoch: 484.1334915161133 Epoch 2/5 Done, Total Loss: 483.99733996035445 Time Elapsed for Epoch: 290.375 seconds PSNR avg = 21.590442620857193,test_loss = 476.878332, score = 0.010975305562484411 starting epoch number 3 Epoch 3......Step: 20/201....... Average Loss for Epoch: 477.1003082275391 Epoch 3......Step: 40/201....... Average Loss for Epoch: 471.6564025878906 Epoch 3......Step: 60/201....... Average Loss for Epoch: 470.134255472819 Epoch 3......Step: 80/201....... Average Loss for Epoch: 473.9191967010498 Epoch 3......Step: 100/201....... Average Loss for Epoch: 478.123049621582 Epoch 3......Step: 120/201....... Average Loss for Epoch: 477.00953572591146 Epoch 3......Step: 140/201....... Average Loss for Epoch: 476.6659194946289 Epoch 3......Step: 160/201....... Average Loss for Epoch: 479.5516366958618 Epoch 3......Step: 180/201....... Average Loss for Epoch: 480.7250172932943 Epoch 3......Step: 200/201....... Average Loss for Epoch: 479.8979231262207 Epoch 3/5 Done, Total Loss: 479.74903391368355 Time Elapsed for Epoch: 307.78125 seconds PSNR avg = 21.587959696538952,test_loss = 476.885648, score = 0.007233724120728361 starting epoch number 4 Epoch 4......Step: 20/201....... Average Loss for Epoch: 471.46647186279296 Epoch 4......Step: 40/201....... Average Loss for Epoch: 467.6473045349121 Epoch 4......Step: 60/201....... Average Loss for Epoch: 466.88021189371744 Epoch 4......Step: 80/201....... Average Loss for Epoch: 471.9590606689453 Epoch 4......Step: 100/201....... Average Loss for Epoch: 476.6317578125 Epoch 4......Step: 120/201....... Average Loss for Epoch: 475.3243273417155 Epoch 4......Step: 140/201....... Average Loss for Epoch: 474.9180716378348 Epoch 4......Step: 160/201....... Average Loss for Epoch: 478.01186027526853 Epoch 4......Step: 180/201....... Average Loss for Epoch: 479.2223595513238 Epoch 4......Step: 200/201....... Average Loss for Epoch: 478.288614654541 Epoch 4/5 Done, Total Loss: 478.1362956032824 Time Elapsed for Epoch: 326.421875 seconds PSNR avg = 21.616568833379915,test_loss = 474.074412, score = 0.006984285357944624 Total Training Time: 1530.765625 seconds
printImages(gif4, preds4)
<ipython-input-7-cda49081dc91>:10: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). gif13[i] = torch.tensor(gif1[i]).numpy().T.astype(np.float32)/255.0 Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Lossy conversion from float32 to uint8. Range [-0.0048330496065318584, 1.0355077981948853]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.0048330496065318584, 1.0355077981948853]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.0048330496065318584, 1.0355077981948853]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.0048330496065318584, 1.0355077981948853]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.0048330496065318584, 1.0355077981948853]. Convert image to uint8 prior to saving to suppress this warning. Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
print_result(tr=metrics4, epochs=5)
class ResidualBlock(nn.Module):
expansion: int = 1
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(in_channels=32,out_channels=32, kernel_size=3, padding=1, dilation=1)
self.conv2 = nn.Conv2d(in_channels=32,out_channels=32, kernel_size=3, padding=2, dilation=2)
self.conv3 = nn.Conv2d(in_channels=32,out_channels=32, kernel_size=3, padding=4, dilation=4)
self.conv4 = nn.Conv2d(in_channels=96,out_channels=32, kernel_size=3, padding=1, dilation=1)
self.relu = nn.LeakyReLU() #inplace=True
def forward(self, x5):
rout = self.conv1(x5)
rout1 = self.conv2(rout)
rout2 = self.conv3(rout1)
rout3 = torch.concat((rout, rout1, rout2), dim=1)
#print(rout3.shape)
rout4 = self.relu(rout3)
rout5 = self.conv4(rout4)
#print(rout4.shape)
return rout5
class Step5Model(nn.Module):
def __init__(self):
super().__init__()
self.resBlock1 = ResidualBlock()
self.resBlock2 = ResidualBlock()
self.resBlock3 = ResidualBlock()
self.conv1 = nn.Conv2d(in_channels=3,out_channels=32, kernel_size=3, padding=1)
self.upsample = nn.Upsample(scale_factor=(144/72)).to(device) #, mode='bilinear'
self.conv2 = nn.Conv2d(in_channels=32,out_channels=3, kernel_size=1)
self.upsample2 = nn.Upsample(scale_factor=(288/144)).to(device)
def forward(self, x, large=False):
out = self.conv1(x)
out1 = self.resBlock1(out)
out2 = self.resBlock2(out1)
out3 = self.upsample(out2)
if large:
out4 = self.resBlock3(out3)
out3 = self.upsample2(out4)
out5 = self.conv2(out3)
return out5
model5 = Step5Model()
gif5, preds5, metrics5 = train(train_loader=train_dataloader, valid_loader=valid_dataloader, learn_rate=0.001, epochs=5, large=False, model=model5)
starting epoch number 0 Epoch 0......Step: 20/201....... Average Loss for Epoch: 4835.81860961914 Epoch 0......Step: 40/201....... Average Loss for Epoch: 3170.7229919433594 Epoch 0......Step: 60/201....... Average Loss for Epoch: 2464.1813435872396 Epoch 0......Step: 80/201....... Average Loss for Epoch: 2047.228160095215 Epoch 0......Step: 100/201....... Average Loss for Epoch: 1779.0008294677734 Epoch 0......Step: 120/201....... Average Loss for Epoch: 1588.1716796875 Epoch 0......Step: 140/201....... Average Loss for Epoch: 1447.6266278948103 Epoch 0......Step: 160/201....... Average Loss for Epoch: 1343.2247299194337 Epoch 0......Step: 180/201....... Average Loss for Epoch: 1257.6064834594727 Epoch 0......Step: 200/201....... Average Loss for Epoch: 1185.8009846496582 Epoch 0/5 Done, Total Loss: 1182.3680160294718 Time Elapsed for Epoch: 733.703125 seconds PSNR avg = 21.15682227499503,test_loss = 524.393725, score = 0.006485407832377151 starting epoch number 1 Epoch 1......Step: 20/201....... Average Loss for Epoch: 527.1337203979492 Epoch 1......Step: 40/201....... Average Loss for Epoch: 514.9008987426757 Epoch 1......Step: 60/201....... Average Loss for Epoch: 509.862570699056 Epoch 1......Step: 80/201....... Average Loss for Epoch: 511.9433765411377 Epoch 1......Step: 100/201....... Average Loss for Epoch: 518.9737524414063 Epoch 1......Step: 120/201....... Average Loss for Epoch: 516.6304916381836 Epoch 1......Step: 140/201....... Average Loss for Epoch: 513.9791560581752 Epoch 1......Step: 160/201....... Average Loss for Epoch: 515.0189790725708 Epoch 1......Step: 180/201....... Average Loss for Epoch: 514.6085795084635 Epoch 1......Step: 200/201....... Average Loss for Epoch: 512.2477124023437 Epoch 1/5 Done, Total Loss: 512.0087377443836 Time Elapsed for Epoch: 693.71875 seconds PSNR avg = 21.437614508755008,test_loss = 493.506613, score = 0.008231479171863308 starting epoch number 2 Epoch 2......Step: 20/201....... Average Loss for Epoch: 492.442333984375 Epoch 2......Step: 40/201....... Average Loss for Epoch: 489.3627075195312 Epoch 2......Step: 60/201....... Average Loss for Epoch: 488.1509343465169 Epoch 2......Step: 80/201....... Average Loss for Epoch: 493.02528381347656 Epoch 2......Step: 100/201....... Average Loss for Epoch: 497.50641693115233 Epoch 2......Step: 120/201....... Average Loss for Epoch: 496.17727127075193 Epoch 2......Step: 140/201....... Average Loss for Epoch: 494.9191356113979 Epoch 2......Step: 160/201....... Average Loss for Epoch: 498.742085647583 Epoch 2......Step: 180/201....... Average Loss for Epoch: 500.0889358520508 Epoch 2......Step: 200/201....... Average Loss for Epoch: 498.4112907409668 Epoch 2/5 Done, Total Loss: 498.2224221300723 Time Elapsed for Epoch: 694.375 seconds PSNR avg = 21.519789969018966,test_loss = 484.379597, score = 0.006485407832377151 starting epoch number 3 Epoch 3......Step: 20/201....... Average Loss for Epoch: 480.9011566162109 Epoch 3......Step: 40/201....... Average Loss for Epoch: 476.1334167480469 Epoch 3......Step: 60/201....... Average Loss for Epoch: 476.18331553141275 Epoch 3......Step: 80/201....... Average Loss for Epoch: 489.0297050476074 Epoch 3......Step: 100/201....... Average Loss for Epoch: 494.86371978759763 Epoch 3......Step: 120/201....... Average Loss for Epoch: 493.2592231750488 Epoch 3......Step: 140/201....... Average Loss for Epoch: 491.9272698538644 Epoch 3......Step: 160/201....... Average Loss for Epoch: 493.8961545944214 Epoch 3......Step: 180/201....... Average Loss for Epoch: 494.14502393934464 Epoch 3......Step: 200/201....... Average Loss for Epoch: 492.557144317627 Epoch 3/5 Done, Total Loss: 492.36727616798817 Time Elapsed for Epoch: 697.984375 seconds PSNR avg = 21.557379980592064,test_loss = 480.231190, score = 0.007732601646295834 starting epoch number 4 Epoch 4......Step: 20/201....... Average Loss for Epoch: 475.8335159301758 Epoch 4......Step: 40/201....... Average Loss for Epoch: 472.17587356567384 Epoch 4......Step: 60/201....... Average Loss for Epoch: 473.8338592529297 Epoch 4......Step: 80/201....... Average Loss for Epoch: 481.10986747741697 Epoch 4......Step: 100/201....... Average Loss for Epoch: 485.2923486328125 Epoch 4......Step: 120/201....... Average Loss for Epoch: 483.6143435160319 Epoch 4......Step: 140/201....... Average Loss for Epoch: 482.86680101667133 Epoch 4......Step: 160/201....... Average Loss for Epoch: 485.82322521209716 Epoch 4......Step: 180/201....... Average Loss for Epoch: 486.64939558241105 Epoch 4......Step: 200/201....... Average Loss for Epoch: 485.47861907958986 Epoch 4/5 Done, Total Loss: 485.3137129598589 Time Elapsed for Epoch: 741.515625 seconds PSNR avg = 21.58617893969386,test_loss = 477.173104, score = 0.0054876527812422055 Total Training Time: 3561.296875 seconds
printImages(gif5, preds5)
<ipython-input-7-cda49081dc91>:10: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). gif13[i] = torch.tensor(gif1[i]).numpy().T.astype(np.float32)/255.0 Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Lossy conversion from float32 to uint8. Range [-0.0029801535420119762, 1.073124885559082]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.0029801535420119762, 1.073124885559082]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.0029801535420119762, 1.073124885559082]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.0029801535420119762, 1.073124885559082]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.0029801535420119762, 1.073124885559082]. Convert image to uint8 prior to saving to suppress this warning. Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
print_result(tr=metrics5, epochs=5)
class Step6Model(nn.Module):
def __init__(self):
super().__init__()
self.vgg = torchvision.models.vgg16(pretrained=True).to(device)
for param in self.vgg.parameters():
param.requires_grad = False
self.extra = self.vgg.features[:3]
self.conv1 = nn.Conv2d(in_channels=3,out_channels=64, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(in_channels=64,out_channels=64, kernel_size=3, padding=1)
self.upsample = nn.Upsample(scale_factor=(144/72), mode='bilinear').to(device) #, mode='bilinear'
self.upsample2 = nn.Upsample(scale_factor=(288/144)).to(device)
self.conv3 = nn.Conv2d(in_channels=128,out_channels=3, kernel_size=1)
self.relu = nn.LeakyReLU()
def forward(self, x, large=False):
out = self.extra(x)
out1 = self.conv1(x)
out2 = self.conv2(out1)
out3 = torch.concat((out2, out), dim=1)
out4 = self.upsample(out3)
out5 = self.relu(out4)
if large:
out5 = self.upsample2(out5)
out6 = self.conv3(out5)
return out6
model6 = Step6Model()
gif6, preds6, metrics6 = train(train_loader=train_dataloader, valid_loader=valid_dataloader, learn_rate=0.001, epochs=5, large=False, model=model6)
starting epoch number 0 Epoch 0......Step: 20/201....... Average Loss for Epoch: 3645.6078918457033 Epoch 0......Step: 40/201....... Average Loss for Epoch: 2443.384484863281 Epoch 0......Step: 60/201....... Average Loss for Epoch: 1937.915737915039 Epoch 0......Step: 80/201....... Average Loss for Epoch: 1645.1158905029297 Epoch 0......Step: 100/201....... Average Loss for Epoch: 1454.7686029052734 Epoch 0......Step: 120/201....... Average Loss for Epoch: 1314.7114430745444 Epoch 0......Step: 140/201....... Average Loss for Epoch: 1209.5963701520648 Epoch 0......Step: 160/201....... Average Loss for Epoch: 1131.3300384521485 Epoch 0......Step: 180/201....... Average Loss for Epoch: 1067.252739461263 Epoch 0......Step: 200/201....... Average Loss for Epoch: 1012.4870770263672 Epoch 0/5 Done, Total Loss: 1009.8391580913791 Time Elapsed for Epoch: 775.90625 seconds PSNR avg = 21.249391091059632,test_loss = 516.059714, score = 0.0037415814417560487 starting epoch number 1 Epoch 1......Step: 20/201....... Average Loss for Epoch: 504.9421127319336 Epoch 1......Step: 40/201....... Average Loss for Epoch: 495.602197265625 Epoch 1......Step: 60/201....... Average Loss for Epoch: 490.98289388020834 Epoch 1......Step: 80/201....... Average Loss for Epoch: 491.9414432525635 Epoch 1......Step: 100/201....... Average Loss for Epoch: 493.4174508666992 Epoch 1......Step: 120/201....... Average Loss for Epoch: 489.82684860229494 Epoch 1......Step: 140/201....... Average Loss for Epoch: 486.90643615722655 Epoch 1......Step: 160/201....... Average Loss for Epoch: 487.8477514266968 Epoch 1......Step: 180/201....... Average Loss for Epoch: 487.4775170220269 Epoch 1......Step: 200/201....... Average Loss for Epoch: 484.94035552978517 Epoch 1/5 Done, Total Loss: 484.70130457332476 Time Elapsed for Epoch: 767.8125 seconds PSNR avg = 21.76381106560845,test_loss = 463.858086, score = 0.0074831628835120975 starting epoch number 2 Epoch 2......Step: 20/201....... Average Loss for Epoch: 458.56356048583984 Epoch 2......Step: 40/201....... Average Loss for Epoch: 453.64890441894534 Epoch 2......Step: 60/201....... Average Loss for Epoch: 452.1695139567057 Epoch 2......Step: 80/201....... Average Loss for Epoch: 455.94326400756836 Epoch 2......Step: 100/201....... Average Loss for Epoch: 459.92005798339846 Epoch 2......Step: 120/201....... Average Loss for Epoch: 458.4446179707845 Epoch 2......Step: 140/201....... Average Loss for Epoch: 457.3000228881836 Epoch 2......Step: 160/201....... Average Loss for Epoch: 459.64657936096194 Epoch 2......Step: 180/201....... Average Loss for Epoch: 460.50176713731554 Epoch 2......Step: 200/201....... Average Loss for Epoch: 459.49795013427735 Epoch 2/5 Done, Total Loss: 459.36940860273825 Time Elapsed for Epoch: 773.34375 seconds PSNR avg = 21.840745337298163,test_loss = 456.447434, score = 0.003492142678972312 starting epoch number 3 Epoch 3......Step: 20/201....... Average Loss for Epoch: 450.0217224121094 Epoch 3......Step: 40/201....... Average Loss for Epoch: 443.8704315185547 Epoch 3......Step: 60/201....... Average Loss for Epoch: 441.8280426025391 Epoch 3......Step: 80/201....... Average Loss for Epoch: 445.5119152069092 Epoch 3......Step: 100/201....... Average Loss for Epoch: 449.5458822631836 Epoch 3......Step: 120/201....... Average Loss for Epoch: 448.13759384155276 Epoch 3......Step: 140/201....... Average Loss for Epoch: 447.2184541974749 Epoch 3......Step: 160/201....... Average Loss for Epoch: 449.80395088195803 Epoch 3......Step: 180/201....... Average Loss for Epoch: 450.925343492296 Epoch 3......Step: 200/201....... Average Loss for Epoch: 450.20613342285156 Epoch 3/5 Done, Total Loss: 450.0794739984161 Time Elapsed for Epoch: 817.828125 seconds PSNR avg = 21.982203869723843,test_loss = 442.771437, score = 0.005737091544025941 starting epoch number 4 Epoch 4......Step: 20/201....... Average Loss for Epoch: 441.10709686279296 Epoch 4......Step: 40/201....... Average Loss for Epoch: 435.80643768310546 Epoch 4......Step: 60/201....... Average Loss for Epoch: 434.3326721191406 Epoch 4......Step: 80/201....... Average Loss for Epoch: 438.55303421020506 Epoch 4......Step: 100/201....... Average Loss for Epoch: 443.1168084716797 Epoch 4......Step: 120/201....... Average Loss for Epoch: 441.9609919230143 Epoch 4......Step: 140/201....... Average Loss for Epoch: 441.2758608136858 Epoch 4......Step: 160/201....... Average Loss for Epoch: 444.0511667251587 Epoch 4......Step: 180/201....... Average Loss for Epoch: 445.36334906684027 Epoch 4......Step: 200/201....... Average Loss for Epoch: 444.53871368408204 Epoch 4/5 Done, Total Loss: 444.39374614354983 Time Elapsed for Epoch: 823.609375 seconds PSNR avg = 22.02286736860541,test_loss = 438.482882, score = 0.006984285357944624 Total Training Time: 3958.5 seconds
printImages(gif6, preds6)
<ipython-input-16-cda49081dc91>:10: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). gif13[i] = torch.tensor(gif1[i]).numpy().T.astype(np.float32)/255.0 Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Lossy conversion from float32 to uint8. Range [-0.052966222167015076, 1.2105000019073486]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.052966222167015076, 1.2105000019073486]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.052966222167015076, 1.2105000019073486]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.052966222167015076, 1.2105000019073486]. Convert image to uint8 prior to saving to suppress this warning. Lossy conversion from float32 to uint8. Range [-0.052966222167015076, 1.2105000019073486]. Convert image to uint8 prior to saving to suppress this warning. Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
print_result(tr=metrics6, epochs=5)
import eagerpy as ep
class Step7Model(nn.Module):
def __init__(self):
super().__init__()
self.vgg = torchvision.models.vgg16(pretrained=True).to(device)
for param in self.vgg.parameters():
param.requires_grad = True
self.extra = self.vgg.features[:3]
self.conv1 = nn.Conv2d(in_channels=3,out_channels=64, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(in_channels=64,out_channels=64, kernel_size=3, padding=1)
#self.upsample = nn.Upsample(scale_factor=(144/72), mode='bilinear').to(device) #, mode='bilinear'
#self.upsample2 = nn.Upsample(scale_factor=(288/144)).to(device)
self.conv3 = nn.Conv2d(in_channels=128,out_channels=3, kernel_size=1)
self.relu = nn.LeakyReLU() #Leaky
def forward(self, x, large=False):
out = self.extra(x)
out1 = self.conv1(x)
out2 = self.conv2(out1)
print(type(out2))
out3 = torch.concat((out2, out), dim=1)
out4 = tf.nn.depth_to_space(out3.detach().numpy(), 6)
#out5 = self.relu(out4)
if large:
out4 = tf.nn.depth_to_space(out4.detach().numpy())
#out4 = out4.numpy().type(torch.tensor)
#out4 = tf.convert_to_tensor(out4.numpy())
out4 = torch.tensor(ep.astensors(out4))
#print(type(out4))
out6 = self.conv3(out4)
return out6
model7 = Step7Model()
gif7, preds7 = train(train_loader=train_dataloader, valid_loader=valid_dataloader, learn_rate=0.001, epochs=5, large=False, model=model7)
printImages(gif7, preds7)
model5_final = Step5Model()
gif5_final, preds5_final, metrics5_final = train(train_loader=train_dataloader, valid_loader=valid_dataloader, learn_rate=0.001, epochs=15, large=False, model=model5_final)
starting epoch number 0 Epoch 0......Step: 20/201....... Average Loss for Epoch: 7477.296142578125 Epoch 0......Step: 40/201....... Average Loss for Epoch: 4715.534033203125 Epoch 0......Step: 60/201....... Average Loss for Epoch: 3595.315604654948 Epoch 0......Step: 80/201....... Average Loss for Epoch: 2943.703719329834 Epoch 0......Step: 100/201....... Average Loss for Epoch: 2519.837977294922 Epoch 0......Step: 120/201....... Average Loss for Epoch: 2219.3422948201496 Epoch 0......Step: 140/201....... Average Loss for Epoch: 1995.659155709403 Epoch 0......Step: 160/201....... Average Loss for Epoch: 1826.6792495727539 Epoch 0......Step: 180/201....... Average Loss for Epoch: 1690.0145056830513 Epoch 0......Step: 200/201....... Average Loss for Epoch: 1576.568703918457 Epoch 0/20 Done, Total Loss: 1571.281909240419 Time Elapsed for Epoch: 670.953125 seconds PSNR avg = 20.946716343010117,test_loss = 547.309746, score = 0.0054876527812422055 starting epoch number 1 Epoch 1......Step: 20/201....... Average Loss for Epoch: 535.9987899780274 Epoch 1......Step: 40/201....... Average Loss for Epoch: 525.9785095214844 Epoch 1......Step: 60/201....... Average Loss for Epoch: 526.1069412231445 Epoch 1......Step: 80/201....... Average Loss for Epoch: 527.464049911499 Epoch 1......Step: 100/201....... Average Loss for Epoch: 530.6085711669922 Epoch 1......Step: 120/201....... Average Loss for Epoch: 527.153960164388 Epoch 1......Step: 140/201....... Average Loss for Epoch: 525.0755007062639 Epoch 1......Step: 160/201....... Average Loss for Epoch: 526.9485935211181 Epoch 1......Step: 180/201....... Average Loss for Epoch: 526.8700473361545 Epoch 1......Step: 200/201....... Average Loss for Epoch: 524.4973130798339 Epoch 1/20 Done, Total Loss: 524.2821787364447 Time Elapsed for Epoch: 660.03125 seconds PSNR avg = 21.335892907683725,test_loss = 504.570329, score = 0.006984285357944624 starting epoch number 2 Epoch 2......Step: 20/201....... Average Loss for Epoch: 502.0793716430664 Epoch 2......Step: 40/201....... Average Loss for Epoch: 495.16686859130857 Epoch 2......Step: 60/201....... Average Loss for Epoch: 497.95703074137367 Epoch 2......Step: 80/201....... Average Loss for Epoch: 504.18539276123045 Epoch 2......Step: 100/201....... Average Loss for Epoch: 507.1325604248047 Epoch 2......Step: 120/201....... Average Loss for Epoch: 504.6456886291504 Epoch 2......Step: 140/201....... Average Loss for Epoch: 503.12623421805245 Epoch 2......Step: 160/201....... Average Loss for Epoch: 505.15613555908203 Epoch 2......Step: 180/201....... Average Loss for Epoch: 506.18612891303167 Epoch 2......Step: 200/201....... Average Loss for Epoch: 504.6064628601074 Epoch 2/20 Done, Total Loss: 504.40498465922343 Time Elapsed for Epoch: 682.6875 seconds PSNR avg = 21.448995346487653,test_loss = 492.494610, score = 0.005737091544025941 starting epoch number 3 Epoch 3......Step: 20/201....... Average Loss for Epoch: 485.69586181640625 Epoch 3......Step: 40/201....... Average Loss for Epoch: 481.03707962036134 Epoch 3......Step: 60/201....... Average Loss for Epoch: 481.4444183349609 Epoch 3......Step: 80/201....... Average Loss for Epoch: 492.2406551361084 Epoch 3......Step: 100/201....... Average Loss for Epoch: 496.55897216796876 Epoch 3......Step: 120/201....... Average Loss for Epoch: 494.8280570983887 Epoch 3......Step: 140/201....... Average Loss for Epoch: 493.9202124459403 Epoch 3......Step: 160/201....... Average Loss for Epoch: 496.0416124343872 Epoch 3......Step: 180/201....... Average Loss for Epoch: 496.63753017849393 Epoch 3......Step: 200/201....... Average Loss for Epoch: 495.2741134643555 Epoch 3/20 Done, Total Loss: 495.11035915393734 Time Elapsed for Epoch: 683.75 seconds PSNR avg = 21.52867321793173,test_loss = 484.149761, score = 0.009977550511349464 starting epoch number 4 Epoch 4......Step: 20/201....... Average Loss for Epoch: 482.78483123779296 Epoch 4......Step: 40/201....... Average Loss for Epoch: 477.19047927856445 Epoch 4......Step: 60/201....... Average Loss for Epoch: 476.522466023763 Epoch 4......Step: 80/201....... Average Loss for Epoch: 480.501602935791 Epoch 4......Step: 100/201....... Average Loss for Epoch: 485.3876556396484 Epoch 4......Step: 120/201....... Average Loss for Epoch: 484.49452921549477 Epoch 4......Step: 140/201....... Average Loss for Epoch: 483.97744227818083 Epoch 4......Step: 160/201....... Average Loss for Epoch: 487.4282033920288 Epoch 4......Step: 180/201....... Average Loss for Epoch: 489.2424555460612 Epoch 4......Step: 200/201....... Average Loss for Epoch: 489.2401419067383 Epoch 4/20 Done, Total Loss: 489.13609777042524 Time Elapsed for Epoch: 740.859375 seconds PSNR avg = 21.54550755968611,test_loss = 482.121328, score = 0.008979795460214518 starting epoch number 5 Epoch 5......Step: 20/201....... Average Loss for Epoch: 489.04075622558594 Epoch 5......Step: 40/201....... Average Loss for Epoch: 480.36111907958986 Epoch 5......Step: 60/201....... Average Loss for Epoch: 477.8958994547526 Epoch 5......Step: 80/201....... Average Loss for Epoch: 480.7203987121582 Epoch 5......Step: 100/201....... Average Loss for Epoch: 484.1881494140625 Epoch 5......Step: 120/201....... Average Loss for Epoch: 483.04277114868165 Epoch 5......Step: 140/201....... Average Loss for Epoch: 482.27984531947544 Epoch 5......Step: 160/201....... Average Loss for Epoch: 485.3051219940186 Epoch 5......Step: 180/201....... Average Loss for Epoch: 486.43724772135414 Epoch 5......Step: 200/201....... Average Loss for Epoch: 485.2787629699707 Epoch 5/20 Done, Total Loss: 485.11917903767295 Time Elapsed for Epoch: 733.625 seconds PSNR avg = 21.591805465559656,test_loss = 477.369656, score = 0.007982040409079572 starting epoch number 6 Epoch 6......Step: 20/201....... Average Loss for Epoch: 472.8657760620117 Epoch 6......Step: 40/201....... Average Loss for Epoch: 469.0232780456543 Epoch 6......Step: 60/201....... Average Loss for Epoch: 468.5591054280599 Epoch 6......Step: 80/201....... Average Loss for Epoch: 475.46062622070315 Epoch 6......Step: 100/201....... Average Loss for Epoch: 480.8699005126953 Epoch 6......Step: 120/201....... Average Loss for Epoch: 479.6551712036133 Epoch 6......Step: 140/201....... Average Loss for Epoch: 479.1038844517299 Epoch 6......Step: 160/201....... Average Loss for Epoch: 481.99797306060793 Epoch 6......Step: 180/201....... Average Loss for Epoch: 483.62206200493705 Epoch 6......Step: 200/201....... Average Loss for Epoch: 482.6035301208496 Epoch 6/20 Done, Total Loss: 482.46276324068134 Time Elapsed for Epoch: 743.65625 seconds
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-11-06e11f6b285e> in <module> 1 model5_final = Step5Model() ----> 2 gif5_final, preds5_final, metrics5_final = train(train_loader=train_dataloader, valid_loader=valid_dataloader, learn_rate=0.001, epochs=20, large=False, model=model5_final) <ipython-input-6-e5db0343aead> in train(train_loader, valid_loader, learn_rate, epochs, large, model) 74 preds = valid_preds 75 else: ---> 76 preds = torch.cat((preds, valid_preds), dim=0) 77 metrics = { 78 'epoch': epoch, RuntimeError: [enforce fail at ..\c10\core\CPUAllocator.cpp:76] data. DefaultCPUAllocator: not enough memory: you tried to allocate 6981230592 bytes.
printImages(gif5_final, preds5_final)
print_result(tr=metrics5_final, epochs=20)